package com.wuyan.locationdog.ui.msg


import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.MsgDetailListAdapter
import com.wuyan.locationdog.databinding.MsgDetailActivityBinding
import com.wuyan.locationdog.help.MsgHelp
import com.wuyan.locationdog.help.finishRefreshAndLoadMoreNoMoreData
import com.wuyan.locationdog.view.ShareAcceptPopup
import com.wuyan.locationdog.viewmodel.MsgViewModel

class MsgDetailActivity : BaseBindingActivity<MsgDetailActivityBinding>() {


    private val mViewModel: MsgViewModel by viewModels()

    private val mAdapter by lazy { MsgDetailListAdapter(R.layout.item_msg_list_detail, null) }

    override fun initData() {
        //用户消息类型1报警 2系统通知

        ///用户消息类型1报警 2共享通知 3团队邀请通知 4指令通知
        when (MsgHelp.instantMsgBeanContent.billType) {
            2 -> {
                setTitleText("共享通知")
            }

            3 -> {
                setTitleText("团队通知")
            }

            4 -> {
                setTitleText("指令通知")
            }
        }
        val map = HashMap<String, Any>()
        map["billType"] = MsgHelp.instantMsgBeanContent.billType
        mViewModel.msgReadAll(map, false)


        binding.apply {

            setOnRefreshLoadMore(smartRefreshLayout)

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无消息", com.arcsoft.commonbase.R.mipmap.empty_msg))

                    addChildClickViewIds(R.id.itemLayout, R.id.btnAccept)
                    setOnItemChildClickListener { adapter, view, position ->

                        if (mAdapter.isDelete) {
//                            data.find { it.isSelect }?.isSelect = false
//                            data[position].isSelect = true
//                            notifyDataSetChanged()
                            data[position].isSelect = !data[position].isSelect
                            notifyItemChanged(position)
                            return@setOnItemChildClickListener
                        }
                        val msgBeanContent = data[position]

                        if (msgBeanContent.billType == 2) {
                            if (msgBeanContent.noticeType == 2) {
                                ShareAcceptPopup(mActivity, msgBeanContent) {
                                    val map = HashMap<String, Any>()
                                    map["shareId"] = msgBeanContent.billId
                                    mViewModel.shareAccept(map)
                                }.showPopupWindow()
                            }
                        } else if (msgBeanContent.billType == 3) {
                            if (msgBeanContent.noticeType == 7) {
                                ShareAcceptPopup(mActivity, msgBeanContent) {
                                    val map = HashMap<String, Any>()
                                    map["teamInvitationId"] = msgBeanContent.billId
                                    mViewModel.teamAccept(map)
                                }.showPopupWindow()
                            }
                        } else {
                            mViewModel.msgRead(mutableListOf(msgBeanContent.userNoticeId))
                            MsgHelp.instantMsgBeanContent = msgBeanContent
                            ActivityUtils.startActivity(MsgDetailMapActivity::class.java)

                        }
                    }
                }
            }

            bottomLayout.setOnClickListener {
                CommonDialog(
                    mActivity,
                    title = "确定删除吗？"
                ) {
                    val list = mAdapter.data.filter { it.isSelect }.map { it.userNoticeId }
                    mViewModel.deleteMsg(list.toMutableList())
                }.show()
            }

        }


    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 20
        map["pageNum"] = currentPage
        map["billType"] = MsgHelp.instantMsgBeanContent.billType
        //用户消息类型1报警 2系统通知
        if (MsgHelp.instantMsgBeanContent.billType == 1) {
            map["deviceId"] = MsgHelp.instantMsgBeanContent.deviceId
        }
        mViewModel.msgDeviceList(map)

    }


    override fun observe() {
        super.observe()

        mViewModel.msgBean.observe(this) {
            finishRefreshAndLoadMoreNoMoreData(binding.smartRefreshLayout, currentPage, it.total)
            if (currentPage == 1) {
                mAdapter.setNewInstance(it.content.toMutableList())
            } else {
                mAdapter.addData(it.content.toMutableList())
            }
        }
        mViewModel.anyBean.observe(this) {
            customShowLong("成功")
            currentPage = 1
            loadNetData()
            Bus.post(bus_device_info_modify, "")
        }
        mViewModel.msgUnReadCountBean.observe(this) {
            binding.apply {

            }
        }
    }
}